<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="区域名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="区域名称"></el-input>
    </el-form-item>
    <el-form-item label="区域上级标识" prop="pid">
      <el-input v-model="dataForm.pid" placeholder="区域上级标识"></el-input>
    </el-form-item>
    <el-form-item label="地名简称" prop="sname">
      <el-input v-model="dataForm.sname" placeholder="地名简称"></el-input>
    </el-form-item>
    <el-form-item label="区域等级" prop="level">
      <el-input v-model="dataForm.level" placeholder="区域等级"></el-input>
    </el-form-item>
    <el-form-item label="区域编码" prop="citycode">
      <el-input v-model="dataForm.citycode" placeholder="区域编码"></el-input>
    </el-form-item>
    <el-form-item label="邮政编码" prop="postcode">
      <el-input v-model="dataForm.postcode" placeholder="邮政编码"></el-input>
    </el-form-item>
    <el-form-item label="组合名称" prop="mername">
      <el-input v-model="dataForm.mername" placeholder="组合名称"></el-input>
    </el-form-item>
    <el-form-item label="citytags 城市标签
value==-1 其他
value==0一线城市
value==1新一线城市
value==2二线城市
value==3三线城市
value==4四线城市
value==5五线城市" prop="citytags">
      <el-input v-model="dataForm.citytags" placeholder="citytags 城市标签
value==-1 其他
value==0一线城市
value==1新一线城市
value==2二线城市
value==3三线城市
value==4四线城市
value==5五线城市"></el-input>
    </el-form-item>
    <el-form-item label="citytags 城市标签
value==-1 其他
value==1一线城市
value==2二线城市
value==3三线城市
value==4四线城市
value==5五线城市" prop="oldcitytags">
      <el-input v-model="dataForm.oldcitytags" placeholder="citytags 城市标签
value==-1 其他
value==1一线城市
value==2二线城市
value==3三线城市
value==4四线城市
value==5五线城市"></el-input>
    </el-form-item>
    <el-form-item label="" prop="lng">
      <el-input v-model="dataForm.lng" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="lat">
      <el-input v-model="dataForm.lat" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="pinyin">
      <el-input v-model="dataForm.pinyin" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="关于城市" prop="aboutcity">
      <el-input v-model="dataForm.aboutcity" placeholder="关于城市"></el-input>
    </el-form-item>
    <el-form-item label="气候背景" prop="climatebackground">
      <el-input v-model="dataForm.climatebackground" placeholder="气候背景"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          pid: '',
          sname: '',
          level: '',
          citycode: '',
          postcode: '',
          mername: '',
          citytags: '',
          oldcitytags: '',
          lng: '',
          lat: '',
          pinyin: '',
          aboutcity: '',
          climatebackground: ''
        },
        dataRule: {
          name: [
            { required: true, message: '区域名称不能为空', trigger: 'blur' }
          ],
          pid: [
            { required: true, message: '区域上级标识不能为空', trigger: 'blur' }
          ],
          sname: [
            { required: true, message: '地名简称不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '区域等级不能为空', trigger: 'blur' }
          ],
          citycode: [
            { required: true, message: '区域编码不能为空', trigger: 'blur' }
          ],
          postcode: [
            { required: true, message: '邮政编码不能为空', trigger: 'blur' }
          ],
          mername: [
            { required: true, message: '组合名称不能为空', trigger: 'blur' }
          ],
          citytags: [
            { required: true, message: 'citytags 城市标签
value==-1 其他
value==0一线城市
value==1新一线城市
value==2二线城市
value==3三线城市
value==4四线城市
value==5五线城市不能为空', trigger: 'blur' }
          ],
          oldcitytags: [
            { required: true, message: 'citytags 城市标签
value==-1 其他
value==1一线城市
value==2二线城市
value==3三线城市
value==4四线城市
value==5五线城市不能为空', trigger: 'blur' }
          ],
          lng: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lat: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pinyin: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          aboutcity: [
            { required: true, message: '关于城市不能为空', trigger: 'blur' }
          ],
          climatebackground: [
            { required: true, message: '气候背景不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/kernel/region/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.region.name
                this.dataForm.pid = data.region.pid
                this.dataForm.sname = data.region.sname
                this.dataForm.level = data.region.level
                this.dataForm.citycode = data.region.citycode
                this.dataForm.postcode = data.region.postcode
                this.dataForm.mername = data.region.mername
                this.dataForm.citytags = data.region.citytags
                this.dataForm.oldcitytags = data.region.oldcitytags
                this.dataForm.lng = data.region.lng
                this.dataForm.lat = data.region.lat
                this.dataForm.pinyin = data.region.pinyin
                this.dataForm.aboutcity = data.region.aboutcity
                this.dataForm.climatebackground = data.region.climatebackground
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/kernel/region/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'pid': this.dataForm.pid,
                'sname': this.dataForm.sname,
                'level': this.dataForm.level,
                'citycode': this.dataForm.citycode,
                'postcode': this.dataForm.postcode,
                'mername': this.dataForm.mername,
                'citytags': this.dataForm.citytags,
                'oldcitytags': this.dataForm.oldcitytags,
                'lng': this.dataForm.lng,
                'lat': this.dataForm.lat,
                'pinyin': this.dataForm.pinyin,
                'aboutcity': this.dataForm.aboutcity,
                'climatebackground': this.dataForm.climatebackground
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
